public class SearchInsertPosition {
    /**
     * @param nums
     * @param target
     * 给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
     * 请必须使用时间复杂度为 O(log n) 的算法。
     * 链接：https://leetcode-cn.com/problems/search-insert-position
     * 解题思路:二分查找
     */
    public static int searchInsert(int[] nums, int target) {
        int low =0;
        int high = nums.length-1;
        int mid = (low+high)/2;
        int loc = 0;
        while (low<=high){
            if (nums[mid] ==target){
                return mid;
            }
            if (nums[mid]>target){
                loc = mid;
                high = mid-1;
                mid = (high+low)/2;
            }
            if (nums[mid]<target){
                loc =mid+1;
                low = mid+1;
                mid = (high+low)/2;


            }
        }

       return  loc;
    }
    public static void main(String[] args) {
        int[] nums = {1,2,3,4,7};
        System.out.println(searchInsert(nums,3));

    }
}
